/*
 * @Author: @yzcheng
 * @Date: 2021-03-02 14:11:37
 * @Version: 1.0
 * @LastEditors: @yzcheng
 * @Description:
 * @LastEditTime: 2021-03-27 13:14:54
 */
import {
  flowTaskList,
  updateTask,
  trunTask,
  undoTask,
  delegateTask,
} from '@/api/flowTaskApi.js'
import { ElMessage } from 'element-plus'
export default {
  namespaced: true,
  state: () => ({
    FlowTaskList: {}, // 流程任务分页数据
    pageNum: 1, //流程任务页数
    pageSize: 10, //流程任务 条数
    key: 1,
    processInfo: null,
    comment: null,
    userCode: null,
    procInsName: '',
    form: {
      procInsName: '',
      name: '',
      userCode: '',
      beginDate: '',
      status: '',
      procDefCategory: '',
      procDefCategoryName: '',
      procDefKey: '',
      procInsId: '',
      procDefId: '',
    },
  }),
  getter: {},
  actions: {
    /**
     *  获取流程任务列表
     *
     * @param {*} { state }
     * @param {*} params
     * @return {*}
     */
    // eslint-disable-next-line no-unused-vars
    async getFlowTaskList({ state }, payload) {
      let { pageNum, pageSize } = state
      pageNum = payload.num
      pageSize = payload.size
      return await flowTaskList(state.form, { pageNum, pageSize })
        .then((res) => {
          res = res.data
          console.log(res,'flowTaskList');
          if (res.success) {
            state.FlowTaskList = res?.data
            state.pageNum = res?.data?.current
            state.total = res?.data?.total
            state.pageSize = res?.data?.size
          }
          return res.code
        })
        .catch(() => {
          return undefined
        })
    },
    /**
     *  流程控制提交
     *
     * @param {*} { state }
     * @param {*} params
     * @return {*}
     */
    // eslint-disable-next-line no-unused-vars
    async selectSub({ state }, payload) {
      const {
        dueDate,
        owner,
        priority,
        name,
        id,
        comment,
        userCode,
      } = state.processInfo
      const data = {
        dueDate,
        owner,
        priority,
        taskName: name,
        taskId: id,
        comment,
        userCode,
      }
      switch (payload) {
        case 1:
          await updateTask(data)
          break
        case 2:
          break
        case 3:
          delete data.owner
          await trunTask(data)
          break
        case 4:
          delete data.priority
          await delegateTask(data)
          break
        case 5:
          break
        case 7:
          delete data.priority
          undoTask({ currentTaskId: id, comment }).then((res) => {
            console.log(res)
          }).catch(err => {
           ElMessage.error('撤办失败，需要当前流程为退回状态')
          })
          break
        case 8:
          // delete data.priority
          // await delegateTask(data)
          break
        default:
          break
      }
    },
    /**
     * 表单重置
     *
     */
    reset({ state, dispatch }) {
      state.form = {
        procInsName: '',
        name: '',
        userCode: '',
        beginDate: '',
        status: '',
        procDefCategory: '',
        procDefKey: '',
        procInsId: '',
        procDefId: '',
      }
      dispatch('getFlowTaskList', {
        num: 1,
        size: 10,
      })
    },
  },
  mutations: {
    /**
     *  流程任务管理 切换页面
     *
     * @param {*} state
     * @param {*} payload
     */
    changeKey(state, payload) {
      state.key = Number(payload?.key) || 1
      state.processInfo = payload?.item || null
    },
    setUserCode(state, payload) {
      state.processInfo.userCode = payload
    },
  },
}
